<template>
  <div class="login_block">
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="账号 :  ">
        <el-input
          v-model="form.account"
          style="width: 220px"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="密码 :  ">
        <el-input
          v-model="form.password"
          style="width: 220px"
          show-password
        ></el-input>
      </el-form-item>
      <el-button type="primary" @click="onSubmit" style="margin-left: 110px">
        登录
      </el-button>
      <el-button style="margin-left: 30px" @click="onNew">注册</el-button>
    </el-form>
  </div>
</template>

<script>
import { login } from "@/api/user";

export default {
  name: "Login",
  data() {
    return {
      form: {
        account: "",
        password: "",
      },
    };
  },
  methods: {
    onSubmit() {
      if (this.form.account != "" && this.form.password != "") {
        if (sessionStorage.getItem("token") === null) {
          login(this.form).then((res) => {

            if (res.msg === "登录成功") {
              this.$message.success(res.msg);
              sessionStorage.setItem("token",res.data);
              this.$router.push("/list");
            }else{
              this.$message.error("账户或密码错误");
            }

          });
        } else {
          this.$router.push("/list");
        }
      } else {
        this.$message.warning("账号或密码不能为空哦!");
      }
    },
    onNew() {
      //跳转到注册页
      this.$router.push("/personform/" + 1);
    },
  },
  beforeDestroy() {},
};
</script>

<style>
.login_block {
  display: inline-block;
  margin-left: 60%;
  margin-top: 5%;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12), 0 0 6px rgba(0, 0, 0, 0.04);
  padding: 50px;
}
</style>